using System;
using CrmSdkHelperClass;
using CrmServiceWebReference;
using KalDev;

/// <summary>
/// Contacts object, to move data between systems
/// </summary>
public class Contact
{
    private string addressLine1 = "";
    private string addressLine2 = "";
    private string addressLine3 = "";
    private string addressLine4 = "";
    private Guid contactIdInCrm;
    private string country = "";
    private string dateOfBirth = "";
    private string emailAddress = "";
    private string firstName = "";
    private string kmsPassword = "";
    private string languagePreference = "";
    private string lastName = "";
    private string mobileNumber = "";
    private string orderNumber = "";
    private string password = "";
    private string personIdNumber = "";
    private string phoneNumber = "";
    private string shopperid = "";
    private string title = "";

    /// <summary>
    /// Gets or sets the address line1.
    /// </summary>
    /// <value>The address line1.</value>
    internal string AddressLine1
    {
        get { return addressLine1; }
        set { addressLine1 = value; }
    }

    /// <summary>
    /// Gets or sets the address line2.
    /// </summary>
    /// <value>The address line2.</value>
    internal string AddressLine2
    {
        get { return addressLine2; }
        set { addressLine2 = value; }
    }

    /// <summary>
    /// Gets or sets the address line3.
    /// </summary>
    /// <value>The address line3.</value>
    internal string AddressLine3
    {
        get { return addressLine3; }
        set { addressLine3 = value; }
    }

    /// <summary>
    /// Gets or sets the address line4.
    /// </summary>
    /// <value>The address line4.</value>
    internal string AddressLine4
    {
        get { return addressLine4; }
        set { addressLine4 = value; }
    }

    /// <summary>
    /// Gets or sets the country.
    /// </summary>
    /// <value>The country.</value>
    internal string Country
    {
        get { return country; }
        set { country = value; }
    }

    /// <summary>
    /// Gets or sets the date of birth.
    /// </summary>
    /// <value>The date of birth.</value>
    internal string DateOfBirth
    {
        get { return dateOfBirth; }
        set { dateOfBirth = value; }
    }

    /// <summary>
    /// Gets or sets the email address.
    /// </summary>
    /// <value>The email address.</value>
    internal string EmailAddress
    {
        get { return emailAddress; }
        set { emailAddress = value; }
    }

    /// <summary>
    /// Gets or sets the name of the first.
    /// </summary>
    /// <value>The name of the first.</value>
    internal string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    /// <summary>
    /// Gets or sets the KMS password.
    /// </summary>
    /// <value>The KMS password.</value>
    internal string KmsPassword
    {
        get { return kmsPassword; }
        set { kmsPassword = value; }
    }

    /// <summary>
    /// Gets or sets the language preference.
    /// </summary>
    /// <value>The language preference.</value>
    internal string LanguagePreference
    {
        get { return languagePreference; }
        set { languagePreference = value; }
    }

    /// <summary>
    /// Gets or sets the name of the last.
    /// </summary>
    /// <value>The name of the last.</value>
    internal string LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }

    /// <summary>
    /// Gets or sets the mobile number.
    /// </summary>
    /// <value>The mobile number.</value>
    internal string MobileNumber
    {
        get { return mobileNumber; }
        set { mobileNumber = value; }
    }

    /// <summary>
    /// Gets or sets the order number.
    /// </summary>
    /// <value>The order number.</value>
    internal string OrderNumber
    {
        get { return orderNumber; }
        set { orderNumber = value; }
    }

    /// <summary>
    /// Gets or sets the password.
    /// </summary>
    /// <value>The password.</value>
    internal string Password
    {
        get { return password; }
        set { password = value; }
    }

    /// <summary>
    /// Gets or sets the person id number.
    /// </summary>
    /// <value>The person id number.</value>
    internal string PersonIdNumber
    {
        get { return personIdNumber; }
        set { personIdNumber = value; }
    }

    /// <summary>
    /// Gets or sets the phone number.
    /// </summary>
    /// <value>The phone number.</value>
    internal string PhoneNumber
    {
        get { return phoneNumber; }
        set { phoneNumber = value; }
    }


    internal string Title
    {
        get { return title; }
        set { title = value; }
    }

    public string ShopperId
    {
        get { return shopperid; }
        set { shopperid = value; }
    }

    public Guid ContactIdInCrm
    {
        get { return contactIdInCrm; }
        set { contactIdInCrm = value; }
    }

    public void CreateInCrm()
    {
        CrmService service = Common.CrmAutorize();
        if (shopperid.Length < 1)
        {
            throw new Exception("Invalid method create, you have to set shopper id");
        }

        Shopper KmsShopper = GetFromKms(ShopperId);

        #region ContactFieldMappings

        contact CrmContactToCreate = new contact();
        CrmContactToCreate.new_street1 = KmsShopper.Address1;
        CrmContactToCreate.new_street2 = KmsShopper.Address2;
        CrmContactToCreate.new_street3 = KmsShopper.Address3;
        CrmContactToCreate.new_street4 = KmsShopper.Address4;
        CrmContactToCreate.mobilephone = KmsShopper.CellPhone;
        CrmContactToCreate.new_country = KmsShopper.Country;
        CrmContactToCreate.birthdate = CrmTypes.CreateCrmDateTime(KmsShopper.Date_of_birth);
        CrmContactToCreate.emailaddress1 = KmsShopper.Email;
        CrmContactToCreate.firstname = KmsShopper.FirstName;
        CrmContactToCreate.new_idnumber = KmsShopper.IdNumber;
        CrmContactToCreate.new_languagepreference = KmsShopper.LanguagePref;
        CrmContactToCreate.lastname = KmsShopper.LastName;
        CrmContactToCreate.new_password = KmsShopper.Password;
        CrmContactToCreate.telephone1 = KmsShopper.Phone;
        CrmContactToCreate.new_shopperid = KmsShopper.ShopperId;

        #endregion ContactFieldMappings

        Guid contactId = service.Create(CrmContactToCreate);
        contactIdInCrm = contactId;

        updateKms();
    }

    public void CreateInCrmWhenUnknown()
    {
        contact crmContactToCreate = new contact();
        crmContactToCreate.firstname = firstName;
        crmContactToCreate.lastname = lastName;
        crmContactToCreate.emailaddress1 = emailAddress;

        CrmService service = Common.CrmAutorize();
        Guid contactid = service.Create(crmContactToCreate);
        contactIdInCrm = contactid;
    }

    /*
    
     private Customer retreiveCustomer(CustomerFeedback sourceFeedbackEntry)
     {
         Guid customerId = new Guid();
         DataTable searchResults = new CombinedSearch().Search(sourceFeedbackEntry.Email);
         if (searchResults.Rows.Count > 0)
         {
             customerId =
                 new Guid(searchResults.Rows[0].ItemArray.GetValue(_searchResultsSystemIdentifierColumnId).ToString());
         }
         else
         {
             if (sourceFeedbackEntry.ShopperId.Length > 1)
             {
                 customerId = Contact.CreateCrmContact(sourceFeedbackEntry.ShopperId);
             }
         }
         return CrmTypes.CreateCustomer(EntityName.contact.ToString(), customerId);
     }
     * 
     */


    public static string createUnknownContact(string email, string FirstName, string LastName)
    {
        CrmService contactService = Common.CrmAutorize();
        contact contact = new contact();
        contact.firstname = FirstName;
        contact.lastname = LastName;
        contact.emailaddress1 = email;
        string keyOfCreatedContact = contactService.Create(contact).ToString();
        return keyOfCreatedContact;
    }

    /// <summary>
    /// Updates KMS, with the ContactId (Guid) so that KMS can return the correct results when integration runs
    /// </summary>
    /// <param name="contactId">The contact id of the CRM Contact</param>
    /// <param name="ShopperId">The shopper id, as KMS knows it</param>
    private void updateKms()
    {
        if (shopperid.Length < 1 || contactIdInCrm.ToString().Length < 1)
        {
            throw new Exception("Invalid update to kms, shopperid and Crm Contact ID has to be specified");
        }

        CRMService service = Common.KmsAuthorize();
        service.UpdateCRMLink(shopperid, contactIdInCrm.ToString());
    }

    /// <summary>
    /// Gets the specified shopper, buy using the KMS Webservice and searching for a shopper id.
    /// If the order of parameters change in the web microsoftCrmWebService, this method will not function correctly
    /// it expects the parameters in the following list (order is important)
    /// <list type="bullet">
    /// <item>Firstname</item>
    /// <item>LastName</item>
    /// <item>Phone Number</item>
    /// <item>Cell number</item>
    /// <item>Email Address</item>
    /// <item>ShopperId</item><remarks>This is the important one</remarks>
    /// <item>OrderID</item>
    /// </list>
    /// </summary>
    /// <param name="ShopperId">The shopper id.</param>
    /// <returns></returns>
    private static Shopper GetFromKms(string ShopperId)
    {
        CRMService service = Common.KmsAuthorize();
        SearchResult result = service.SearchShoppers("", "", "", "", "", ShopperId, "");
        return result.Shoppers[0];
    }
}